package cn.net.shuxing.yifu.common.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.time.*;
import java.util.Date;

public class LocalDateTimeUtil {

    public static LocalDateTime createLocalDateTime(LocalDate localDate, LocalTime localTime) {
        Class<LocalDateTime> localDateTimeClass = LocalDateTime.class;
        try {
            Constructor<?> constructor = localDateTimeClass.getDeclaredConstructors()[0];
            constructor.setAccessible(true);
            LocalDateTime localDateTime = (LocalDateTime) constructor.newInstance(localDate, localTime);
            return localDateTime;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date toDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    public static LocalDateTime getThisMonthFirstDay() {
        return createLocalDateTime(LocalDateUtil.getThisMonthFirstDay(), LocalTimeUtil.createLocalTimeInZero());
    }

    public static LocalDate getLocalDate(LocalDateTime localDateTime)
    {
        try {
            Field date = LocalDateTime.class.getDeclaredField("date");
            date.setAccessible(true);
            LocalDate localDate = (LocalDate) date.get(localDateTime);
            return localDate;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static LocalDateTime getToday() {
        return createLocalDateTime(LocalDate.now(),LocalTimeUtil.createLocalTimeInZero());
    }
}
